package com.abc.example.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.gitee.shengzheng1999.esbcommon.common.utils.LogUtil;

import com.abc.example.dao.OrgnizationDao;
import com.abc.example.dao.UserDao;
import com.abc.example.entity.Orgnization;
import com.abc.example.enumeration.EIdType;
import com.abc.example.exception.BaseException;
import com.abc.example.exception.ExceptionCodes;
import com.abc.example.service.CacheDataService;
import com.abc.example.service.GlobalConfigService;
import com.abc.example.service.IdCheckService;
import com.abc.example.service.OrgnizationService;

/**
 * @className		: IdCheckServiceImpl
 * @description	: IdCheckService实现类
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2023/04/08	1.0.0		sheng.zheng		初版
 *
 */
@Service
public class IdCheckServiceImpl implements IdCheckService{
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private OrgnizationDao orgnizationDao;	
	
	// 缓存数据服务类对象
	@Autowired
	private CacheDataService cds;
	
	// 公共配置数据服务类对象
	@Autowired
	private GlobalConfigService gcs;
		
	
	/**
	 * 
	 * @methodName		: getObjById
	 * @description	: 检查对象ID的有效性，如果有效则返回对象，如果无效，则抛出异常
	 * @remark			: 参见接口类相应方法说明
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2023/04/08	1.0.0		sheng.zheng		初版
	 *
	 */
	@Override
	public Object getObjById(String clazzName,Object id,Object...params) {
		Object item = null;
		String idName = "";
		try {
			switch(clazzName) {
			case "User":
			{
				idName = "userId";
				item = userDao.selectItemByKey((Long)id);
			}
				break;
			case "Orgnization":
			{
				idName = "orgId";
				Orgnization org = cds.getItem("Orgnization", id);
				item = org;
			}
				break;
			default:
				break;		
			}		
		}catch(Exception e) {
			LogUtil.error(e);
			throw new BaseException(ExceptionCodes.ERROR,e.getMessage());
		}
		if (item == null) {
			throw new BaseException(ExceptionCodes.OBJECT_DOES_NOT_EXIST,
					clazzName + " " + idName + " = " + id);
		}			
		return item;
	}
	
	/**
	 * 
	 * @methodName		: getObjByUniqueKey
	 * @description	: 根据唯一键或准唯一键获取对象
	 * @see {@link IdCheckService#getObjByUniqueKey}
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2024/01/27	1.0.0		sheng.zheng		初版
	 *
	 */
	public Object getObjByUniqueKey(String clazzName,String propName,Object propValue) {
		Object item = null;
		switch(clazzName) {
		case "Orgnization":
		{
			switch(propName){
			case "orgName":
			case "parentOrgName":
			{
				String orgName = (String)propValue;
				Orgnization org = orgnizationDao.selectItemByOrgName(orgName);
				item = org;
			}
				break;
			default:
				break;
			}
		}
			break;
		default:
			break;
		}
		return item;
	}
	
	/**
	 * 
	 * @methodName		: getObjIdByName
	 * @description	: 根据对象名称获取对象的ID
	 * @see {@link IdCheckService#getObjIdByName}
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2024/05/15	1.0.0		sheng.zheng		初版
	 *
	 */
	public Object getObjIdByName(String clazzName,String propName,Object propValue) {
		Object oItem = getObjByUniqueKey(clazzName,propName,propValue);
		if (oItem == null) {
			return null;			
		}
		Object oVal = null;
		switch(clazzName) {
		case "Orgnization":
			oVal = ((Orgnization)oItem).getOrgId();
			break;
		default:
			break;
		}
		return oVal;
	}	
}
